package com.example.yunxiappbackend.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.yunxiappbackend.pojo.Article;
import com.example.yunxiappbackend.pojo.Fenxi;
import com.example.yunxiappbackend.pojo.User;
import com.example.yunxiappbackend.service.ArticleService;
import com.example.yunxiappbackend.service.FenxiService;
import com.example.yunxiappbackend.service.UserService;
import com.example.yunxiappbackend.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/backgroundData")
public class BackgroundDataController {
    @Autowired
    UserService userService;
    @Autowired
    FenxiService fenxiService;
    @Autowired
    ArticleService articleService;

    //user------------------------------------------------
    @GetMapping("/user")
    public Result<List<User>> getUser(){
        List<User> list = userService.list();
        return Result.success(list);
    }
    @GetMapping("/user/{id}")
    public Result<User> getOneUser(@PathVariable String id) {
        User user = userService.getById(id);
        return Result.success(user);
    }
    @PutMapping("/user")
    public Result<String> updateUser(@RequestBody User user){
        userService.updateById(user);
        return Result.success("更新成功");
    }
    @DeleteMapping("/user/{id}/{status}")
    public Result<String> deleteUser(@PathVariable String id,@PathVariable int status){
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(User::getStatus,status).eq(User::getId,id);
        userService.update(updateWrapper);
        String r = status == 0 ? "账户已禁用":"账户已启用";
        return Result.success(r+"，刷新页面后可见");
    }
    //fenxi-------------------------------------------------
    @GetMapping("/fenxi")
    public Result<List<Fenxi>> getFenxi(){
        List<Fenxi> list = fenxiService.list();
        System.out.println(list);
        return Result.success(list);
    }
    @GetMapping("/fenxi/{id}")
    public Result<Fenxi> getOne(@PathVariable String id){
        Fenxi fenxi = fenxiService.getById(id);
        return Result.success(fenxi);
    }
    @PostMapping("/fenix")
    public Result<String> save(@RequestBody Fenxi fenxi){
        if(fenxi.getId() == null){
            fenxiService.save(fenxi);
            return Result.success("添加成功");
        }else {
            fenxiService.updateById(fenxi);
            return Result.success("修改成功");
        }
    }
    //update方法整合到insert中 id为null即新增 反之更新
//    @PutMapping("/fenxi")
//    public Result<String> update(@RequestBody Fenxi fenxi){
//        fenxiService.updateById(fenxi);
//        return Result.success("修改成功");
//    }
    @DeleteMapping("/fenxi/{id}")
    public Result<String> deleteFenxi(@PathVariable String id){
        fenxiService.removeById(id);
        return Result.success("删除成功");
    }

    //article-------------------------------------------
    @GetMapping("/article")
    public Result<List<Article>> getAll(){
        List<Article> list = articleService.list();
        return Result.success(list);
    }
    @GetMapping("/article/{id}")
    public Result<Article> getArticleById(@PathVariable("id") String id) {
        Article a = articleService.getById(id);
        return Result.success(a);
    }
    @PostMapping("/article")
    public Result<String> save(@RequestBody Article article){
        System.out.println(article);
        if(article.getId()!=null){ //更新
            articleService.updateById(article);
            return Result.success("更新成功");
        }else { //新增
            articleService.save(article);
            return Result.success("添加成功");
        }
    }
    @DeleteMapping("/article/{id}")
    public Result<String> deleteArticle(@PathVariable String id){
        articleService.removeById(id);
        return Result.success("删除成功");
    }
}
